package com.example.first.adapters;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.first.R;
import com.example.first.modle.DishFlavor;
import com.google.android.flexbox.FlexboxLayout;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FlavorAdapter extends RecyclerView.Adapter<FlavorAdapter.FlavorViewHolder> {

    private Context context;
    private List<DishFlavor> dishFlavors;
    private Map<Integer, Integer> selectedPositions; // 记录每个口味组中选中的按钮位置

    public FlavorAdapter(Context context, List<DishFlavor> dishFlavors) {
        this.context = context;
        this.dishFlavors = dishFlavors;
        this.selectedPositions = new HashMap<>();
        initializeDefaultSelections();
    }

    public void setDishFlavors(List<DishFlavor> dishFlavors) {
        this.dishFlavors = dishFlavors;
        initializeDefaultSelections();
        notifyDataSetChanged();
    }

    private void initializeDefaultSelections() {
        for (int i = 0; i < dishFlavors.size(); i++) {
            selectedPositions.put(i, 0); // 默认选择每个口味组的第一个按钮
        }
    }

    @NonNull
    @Override
    public FlavorViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.flavor_item, parent, false);
        return new FlavorViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull FlavorViewHolder holder, int position) {
        DishFlavor dishFlavor = dishFlavors.get(position);
        holder.titleTextView.setText(dishFlavor.getName());

        FlexboxLayout buttonGroup = holder.buttonGroup;
        buttonGroup.removeAllViews(); // 清空之前的按钮

        // 使用Gson将类似["不要葱","不要蒜","不要香菜","不要辣"]的数据解析为List<String>
        Gson gson = new Gson();
        Type listType = new TypeToken<List<String>>(){}.getType();
        List<String> flavorGroup = gson.fromJson(dishFlavor.getValue(), listType);

        for (int i = 0; i < flavorGroup.size(); i++) {
            Button button = new Button(context);
            button.setText(flavorGroup.get(i));
            button.setPadding(16, 16, 16, 16);
            button.setTextSize(14);
            button.setBackgroundColor(Color.parseColor("#FFC300"));

            final int index = i;
            button.setOnClickListener(v -> {
                selectedPositions.put(position, index);
                notifyDataSetChanged();
            });

            // 设置选中状态
            if (selectedPositions.getOrDefault(position, -1) == index) {
                button.setBackgroundColor(Color.parseColor("#FFC300"));
            } else {
                button.setBackgroundColor(context.getResources().getColor(android.R.color.white));
            }

            // 设置按钮的外边距
            FlexboxLayout.LayoutParams layoutParams = new FlexboxLayout.LayoutParams(
                    FlexboxLayout.LayoutParams.WRAP_CONTENT,
                    FlexboxLayout.LayoutParams.WRAP_CONTENT
            );
            layoutParams.setMargins(8, 8, 8, 8); // 设置按钮之间的间距
            button.setLayoutParams(layoutParams);

            buttonGroup.addView(button);
        }
    }

    // 获取按钮文本
    public String getSelectedButtonText() {
        StringBuilder selectedTexts = new StringBuilder();
        for (Map.Entry<Integer, Integer> entry : selectedPositions.entrySet()) {
            int position = entry.getKey();
            int index = entry.getValue();
            if (position >= 0 && position < dishFlavors.size()) {
                DishFlavor dishFlavor = dishFlavors.get(position);
                Gson gson = new Gson();
                Type listType = new TypeToken<List<String>>() {}.getType();
                List<String> flavorGroup = gson.fromJson(dishFlavor.getValue(), listType);
                if (index >= 0 && index < flavorGroup.size()) {
                    selectedTexts.append(flavorGroup.get(index)).append(", ");
                }
            }
        }
        if (selectedTexts.length() > 0) {
            selectedTexts.setLength(selectedTexts.length() - 2); // 去掉最后一个逗号和空格
        }
        return selectedTexts.toString();
    }

    @Override
    public int getItemCount() {
        return dishFlavors != null ? dishFlavors.size() : 0;
    }

    static class FlavorViewHolder extends RecyclerView.ViewHolder {
        TextView titleTextView;
        FlexboxLayout buttonGroup;

        FlavorViewHolder(View itemView) {
            super(itemView);
            titleTextView = itemView.findViewById(R.id.titleTextView);
            buttonGroup = itemView.findViewById(R.id.buttonGroup);
        }
    }

    public Map<Integer, Integer> getSelectedPositions() {
        return selectedPositions;
    }
}
